﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.FileManipulation.Properties;

namespace Workflows.Components.FileManipulation
{
    /// <summary>
    /// folder sync
    /// </summary>
    [Serializable]
    public class FolderSyncComponent:WorkflowComponentBase
    {
        #region members
        private FolderSyncInstruction _Instruction;
        private FolderSyncOutput _Output;

        public override InstructionBase Instruction
        {
            get { return _Instruction; }
            set { _Instruction =(FolderSyncInstruction) value; }
        }

        public override ResultBase Output
        {
            get { return _Output; }
            set { _Output =(FolderSyncOutput) value; }
        }

        /// <summary></summary>
        public string SrcFolderPath
        {
            get
            {
                return this._Instruction.SrcFolderPath;
            }
            set
            {
                this._Instruction.SrcFolderPath = value;
            }
        }

        /// <summary></summary>
        public string TargetFolderPath
        {
            get
            {
                return this._Instruction.TargetFolderPath;
            }
            set
            {
                this._Instruction.TargetFolderPath = value;
            }
        }

        /// <summary></summary>
        public string ExcludedFileExtensions
        {
            get
            {
                return this._Instruction.ExcludedFileExtensions;
            }
            set
            {
                this._Instruction.ExcludedFileExtensions = value;
            }
        }

        /// <summary></summary>
        public bool IncludeSubFolders
        {
            get
            {
                return this._Instruction.IncludeSubFolders;
            }
            set
            {
                this._Instruction.IncludeSubFolders = value;
            }
        }
        #endregion

        /// <summary>
        /// ctor
        /// </summary>
        public FolderSyncComponent()
            : base()
        {
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.File_Tasks.ToString();
            this.Description = "Synchronize two folders";
            this.ComponentIcon = Resources.cscui_1200;
            this.ComponentImage = Resources.FolderSync;
            this.ComponentName = "Folder Sync";

            this._Instruction =
                new FolderSyncInstruction(this.ComponentID, Guid.Empty, string.Empty, string.Empty, string.Empty, true);
            this._Output = new FolderSyncOutput(this.ComponentID, Guid.Empty, 0, 0, 0);
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SourceFolderPath", this.SrcFolderPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TargetFolderPath", this.TargetFolderPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ExcludedFileExtensions", this.ExcludedFileExtensions);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "IncludeSubFolders", this.IncludeSubFolders.ToString());

            return xNode;
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                FolderSyncComponent component = base.Instantiate(xNode) as FolderSyncComponent;

                component.SrcFolderPath = xNode.Attributes.GetNamedItem("SourceFolderPath").Value;
                component.TargetFolderPath = xNode.Attributes.GetNamedItem("TargetFolderPath").Value;
                component.ExcludedFileExtensions = xNode.Attributes.GetNamedItem("ExcludedFileExtensions").Value;
                component.IncludeSubFolders = bool.Parse(xNode.Attributes.GetNamedItem("IncludeSubFolders").Value);

                return component;
            }
            else
            {
                throw new Exception("Invalid type to instantiate");
            }
        }
    }
}
